package com.atguigu.gulimall.cart.vo;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 *
 * @author 孤风雪影
 * @date 2021/1/18
 */
@Data
public class Cart {

    List<CartItem> items;

    //以下都是计算属性
    private Integer countType; //商品类型
    private Integer countNum; //商品数量
    private BigDecimal totalAmount; //商品总价

    private BigDecimal reduce = new BigDecimal("0.00"); //减免价格

    public Integer getCountType() {
        int count = 0;
        if (!CollectionUtils.isEmpty(items)) {
            count = items.size();
        }
        return count;
    }

    public Integer getCountNum() {
        int count = 0;
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0.00");

        //1.计算购物项总价
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                //选中的
                if (item.getCheck()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }

        //2.减去优惠总价
        amount = amount.subtract(this.reduce);

        return amount;
    }
}
